package com.lac.component.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author xiaokuixiao
 * date2025/7/4 14:08
 */
public class FixedThreadPoolExample {

    public static void main(String[] args) {
        // 创建一个固定大小为3的线程池
        ExecutorService executor = Executors.newFixedThreadPool(3);

        // 提交多个任务
        for (int i = 1; i <= 6; i++) {
            final int taskId = i;
            executor.submit(() -> {
//                System.out.println("任务 " + taskId + "开始，线程：" + Thread.currentThread().getName());
                try {
                    Thread.sleep(2000); // 模拟任务执行
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                }
//                System.out.println("任务 " + taskId + "完成");
            });
        }
        executor.shutdown();
    }
}
